<template>
  <el-form ref="postForm" :model="postForm" :rules="rules">
    <div style="padding: 40px 45px 20px 50px;">
      <el-row>
        <el-col :span="14">
          <el-form-item label="标题" prop="name">
            <el-input v-model.trim="postForm.name" />
          </el-form-item>
          <el-form-item label="序号">
            <el-input v-model.trim="postForm.sequence" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="轮播图(尺寸:375 * 163):" prop="image" style="margin-left: 30px;">
            <Upload :default-image.sync="postForm.image" :mode="6" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="submit" >确定</el-button>
      </el-form-item>
    </div>
  </el-form>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload'
import { newsSave, newsGetById } from '@/api/website'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'

const defaultForm = {
  id: 0,
  sequence: '',
  code: "weiIndexRoll",
  productId: '',
  identifier: '',
  image: '',
  name: '',
  num: '',
  remark: '',
  paramType: 99,
}
export default {
  name: 'MicroIndexRollSave',
  directives: { waves },
  components: { Tinymce, Upload, Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'danger',
        1: 'success',
        2: '',
        3: 'info'
      }
      return statusMap[status]
    }
  },
  props: {
  },
  data() {
    return {
      postForm: Object.assign({}, defaultForm),
      rules: {
        image: [{ required: true, message: '请选择', trigger: 'blur' }],
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        remark: [{ required: true, message: '请输入图文详情', trigger: 'blur' }],
      },
      isEdit: false,
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.isEdit = this.$route.query && this.$route.query.id && Number(this.$route.query.id) > 0
      if (this.isEdit) {
        this.fetchData(this.$route.query.id)
      }
    },
    fetchData(id) {
      newsGetById(id).then(response => {
        this.postForm = Object.assign({}, response)
        if (this.postForm.remark) {
          this.$refs.editor.setContent(this.postForm.remark)
        }
      }).catch(err => {
        console.log(err)
      })
    },
    submit() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          let params = Object.assign({}, this.postForm)
          console.log(89888, params)
          delete params.createTime

          newsSave(params, this.isEdit).then((res) => {
            this.$notify({
              title: '成功',
              message: `${this.isEdit ? '编辑' : '添加'}成功`,
              type: 'success',
              duration: 2000
            })
            // 调用全局挂载的方法,关闭当前标签页
            this.$store.dispatch("tagsView/delView", this.$route);
            this.$router.push({
              path: '/microShop/microIndexRoll',
              query: {
                t: Date.now()
              }
      });
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>

</style>